package com.hlg.xboot.web.controller;

import com.hlg.xboot.enums.DicType;
import com.hlg.xboot.model.BaseDic;
import com.hlg.xboot.model.BaseDicTree;
import com.hlg.xboot.service.DicsService;
import com.hlg.xboot.vo.RestBody;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by tzw on 2017/5/3.
 */
@RestController
@RequestMapping("/dics")
public class DicsController extends BaseController {

    @Autowired
    private DicsService dicsService;

    /**
     * 取多个字典表数据集合
     * @param dicNames
     * @return
     * @throws Exception
     */
    @GetMapping()
    public RestBody getDics(String[] dicNames) throws Exception {
        Map<String, List<Map<String, Object>>> data = new HashedMap();
        for (String dicName : dicNames) {
            List<Map<String, Object>> dicData = null;
            String dicKey = "dicData."+dicName;
            if(redisTemplate.hasKey(dicKey)){
                dicData = (List<Map<String, Object>>)redisTemplate.opsForValue().get(dicKey);
            }else{
                dicData = getDicData(dicName);
                redisTemplate.opsForValue().set(dicKey, dicData);
            }
            data.put(dicName, dicData);
        }
        return success(data);
    }

    /**
     * 取单个字典表数据
     * @param dicName
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/{dicName}")
    public RestBody getDics(@PathVariable String dicName) throws Exception {
        String dicKey = "dicData."+dicName;
        if(redisTemplate.hasKey(dicKey)){
            return success(redisTemplate.opsForValue().get(dicKey));
        }else{
            List<Map<String, Object>> data = getDicData(dicName);
            redisTemplate.opsForValue().set(dicKey, data);
            return success(data);
        }
    }

    private List<Map<String, Object>> getDicData(String dicName) throws Exception {
        switch (DicType.getItem(dicName)){
            case Country:
            case Nation:
            case Party:
            case ProTitle:
            case LeaderUnit:
                return getBaseDicData(dicName);
            case District:
            case Subject:
            case NationCode:
                return getBaseDicTreeData(dicName);
            default:
                return new ArrayList<>();
        }
    }

    private List<Map<String, Object>> getBaseDicData(String dicName) throws Exception {
        List<BaseDic> list = dicsService.queryBaseDicByName(dicName);
        if(list == null){
            return new ArrayList<>();
        }
        return list.stream().map((item) -> item.transformMapModel()).collect(Collectors.toList());
    }

    private List<Map<String, Object>> getBaseDicTreeData(String dicName) throws Exception {
        List<BaseDicTree> list = dicsService.queryBaseDicTreeByName(dicName);
        return dicsService.transTreeData(list);
    }

}
